package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bigchun.woodhunter.R;

import java.util.List;

import bean.ContentDataBean;
import bean.GoodsBean;
import myapp.MyApp;
import util.DensityUtil;
import util.GlideUtils;

/**
 * Created by fei on 2017/12/30.
 */

public class GoodsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_BANNER = 0;//头部轮播图
    public static final int TYPE_CLASSIFICATION = 1;//分类
    public static final int TYPE_SPECIAL = 2;//通知和专题页
    public static final int TYPE_ADVERT = 3;//广告
    public static final int TYPE_PREFERRED = 4;//为你优选
    public static final int TYPE_NORMAL = 5;
    public static final int TYPE_BOTTOM = 6;//底部

    private List<GoodsBean.DataBean> mDatas;
    private Context context;
    private MyApp myApp;
    private int width;
    private ContentDataBean.DataBean textContent;

    public GoodsRecyclerViewAdapter(List<GoodsBean.DataBean> mDatas, Context context, MyApp myApp, int width, ContentDataBean.DataBean content ) {
        this.mDatas = mDatas;
        this.context = context;
        this.myApp = myApp;
        this.width = width;
        this.textContent = content;
    }

    private View bottomView;

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener li) {
        mListener = li;
    }






    public void setBottomView(View bottomView) {
        this.bottomView = bottomView;
    }

    public void setDatas(List<GoodsBean.DataBean> datas) {
        this.mDatas = datas;
//        notifyDataSetChanged();
//        notifyItemRangeChanged(datas.size()-10,10);
        if (datas.size() > 20){
            notifyItemRangeChanged(datas.size()-20,20);
        }else{
//            notifyDataSetChanged();
            notifyItemRangeChanged(0,datas.size()-1);
        }
    }

    @Override
    public int getItemViewType(int position) {

        if (position == mDatas.size()) return TYPE_BOTTOM;
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        if (bottomView != null && viewType == TYPE_BOTTOM) {
            return new Holder(bottomView);

        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goodfragment, parent, false);
        return new Holder(layout);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {

        if (getItemViewType(position) == TYPE_BOTTOM) {
            return;
        }
        final int pos = getRealPosition(viewHolder);
        final GoodsBean.DataBean bean = mDatas.get(pos);
        if (viewHolder instanceof Holder) {

            int twoimgwidth = width-DensityUtil.dip2px(context,48);
            int oneimgwidth = twoimgwidth/2;
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) ((Holder) viewHolder).ll_img.getLayoutParams();
            layoutParams.width = oneimgwidth;
            ((Holder) viewHolder).ll_img.setLayoutParams(layoutParams);


            GlideUtils.loadRoundCircleImage(context, bean.getImage()+"?x-oss-process=image/resize,w_420", ((Holder) viewHolder).img);
            /*((Holder)viewHolder).img.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    saveImagUtil.showDiaLog(context,bean.getImage(),textContent);
                    return true;
                }
            });*/
            ((Holder) viewHolder).tv_name.setText(bean.getStore_name());

            if (!"".equals(bean.getPrice())){
                ((Holder) viewHolder).tv_price.setText("￥"+bean.getPrice());
                ((Holder) viewHolder).tv_price.setVisibility(View.VISIBLE);
            }else{
                ((Holder) viewHolder).tv_price.setVisibility(View.GONE);
            }


            if (mListener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.onItemClick(pos);
                    }
                });
            }
            ((Holder) viewHolder).ll_shuxing.removeAllViews();
            for (int m = 0;m<bean.getShuxing().size();m++){
                View view = View.inflate(context,R.layout.item_shuhxing,null);
                TextView tv_name = view.findViewById(R.id.tv_name);
                TextView tv_value = view.findViewById(R.id.tv_value);
                tv_name.setText(bean.getShuxing().get(m).getName()+"："+bean.getShuxing().get(m).getValue());
//                tv_value.setText(bean.getShuxing().get(m).getValue());
                if (!"".equals(bean.getShuxing().get(m)) && !"".equals(bean.getShuxing().get(m).getValue())){
                    ((Holder) viewHolder).ll_shuxing.addView(view);
                }
            }
        }
    }





    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return position;
    }

    @Override
    public int getItemCount() {
        return mDatas.size() + 1;
    }

    class Holder extends RecyclerView.ViewHolder {

        private ImageView img;//店铺图片
        private TextView tv_name;//超出配送范围
        private TextView tv_price;//超出配送范围
        private RelativeLayout ll_img;//超出配送范围
        private LinearLayout ll_shuxing;//超出配送范围

        public Holder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            ll_img = (RelativeLayout) itemView.findViewById(R.id.ll_img);
            ll_shuxing = (LinearLayout) itemView.findViewById(R.id.ll_shuxing);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }



}